﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Xc.DataAccess.Docs.Services
{
    /// <summary>
    /// Pandoc服务
    /// </summary>
    internal class PanDocService : IPanDocService
    {
        /// <summary>
        /// 打开导出的文件
        /// </summary>
        public void OpenExportFile()
        {
            Process.Start("explorer", file);
        }
        /// <summary>
        /// 可执行文件路径
        /// </summary>
        private readonly string DocPath = Path.Combine(Directory.GetParent(typeof(PanDocService).Assembly.Location).FullName, "Resources", "pandoc.exe");
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public Process Execute(string parameter)
        {
            return Process.Start(DocPath, parameter);
        }
        /// <summary>
        /// 使用模板将文件导出为指定格式的文件
        /// </summary>
        /// <param name="template">模板文件</param>
        /// <param name="sourceFile">原始文件</param>
        /// <param name="exportFile">导出文件</param>
        /// <returns></returns>
        public Process DocxByTemplate(string template, string sourceFile, string exportFile)
        {
            file = exportFile;

            return Execute($" --reference-doc {template} {sourceFile} -o {exportFile}");
        }
        /// <summary>
        /// 导出为pdf文件
        /// </summary>
        /// <param name="template"></param>
        /// <param name="sourceFile"></param>
        /// <param name="exportFile"></param>
        /// <returns></returns>
        public Process PdfByTemplate(string template, string sourceFile, string exportFile)
        {
            file = exportFile;
            return Execute($"-pdf-engine=xelatex -template={template} {sourceFile} -o {exportFile}");
        }
        /// <summary>
        /// 导出模板文件
        /// </summary>
        /// <param name="exportTemplateFile"></param>
        /// <returns></returns>
        public Process DocxExportTemplate(string exportTemplateFile)
        {
            return Execute($" -o {exportTemplateFile} --print-default-data-file reference.docx");
        }
        private string file;
        /// <summary>
        /// 直接导出
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="exportFile"></param>
        /// <returns></returns>
        public Process Docx(string sourceFile, string exportFile)
        {
            file = exportFile;
            return Execute($" {sourceFile} -o {exportFile} --toc -s --toc-depth=4");
        }
        public Process Pdf(string sourceFile, string exportFile)
        {
            file = exportFile;
            return Execute($" {sourceFile} -o {exportFile} --latex-engine=xelate -t pdf");
        }
    }
}
